﻿using System;
using System.Configuration;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page 
{
    private messagesService mSrvc;

    private string messageFormat = "<div class=\"message\" style=\"font-size:{0}pt;\"><a href='{1}' target=\"_top\">{2}</a></div>";
        
    /// <summary>
    /// Высота вьюпорта для просмотра сообщений
    /// </summary>
    protected int ViewportHeight
    {
        get
        {
            int vHeight = 600;
            if (!String.IsNullOrEmpty(Request["height"]))
            {
                int.TryParse(Request["height"], out vHeight);
            }

            return vHeight;
        }
    }

    /// <summary>
    /// Ширина вьюпорта для просмотра сообщений
    /// </summary>
    protected int ViewportWidth
    {
        get
        {
            int vWidth = 500;
            if (!String.IsNullOrEmpty(Request["width"]))
            {
                int.TryParse(Request["width"], out vWidth);
            }

            return vWidth;
        }
    }

    protected string FeedUrl
    {
        get
        {
            //return "http://anotheronestory.blogspot.com/feeds/posts/default";

            if (string.IsNullOrEmpty(Request["feedurl"]))
                return string.Empty;

            return Request["feedurl"];
        }
    }

    protected string LinkUrl
    {
        get
        {
            //return "http://anotheronestory.blogspot.com/search/label/";

            if (string.IsNullOrEmpty(Request["linkurl"]))
                return string.Empty;

            return Request["linkurl"];
        }
    }

    private int MinFont
    {
        get
        {
            int mFont = 8;
            if (!String.IsNullOrEmpty(Request["minfont"]))
            {
                int.TryParse(Request["minfont"], out mFont);
            }

            return mFont;
        }
    }

    private int MaxFont
    {
        get
        {
            int mFont = 24;
            if (!String.IsNullOrEmpty(Request["maxfont"]))
            {
                int.TryParse(Request["maxfont"], out mFont);
            }

            return mFont;
        }
    }

    protected bool RandomizePosition
    {
        get
        {
            bool randpos = false;
            if (!String.IsNullOrEmpty(Request["randpos"]))
            {
                bool.TryParse(Request["randpos"], out randpos);
            }

            return randpos;
        }
    }

    protected string FontColor
    {
        get
        {
            if (!String.IsNullOrEmpty(Request["fontcol"]))
            {
                return Request["fontcol"];
            }

            return "#00f";
        }
    }

    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        if (mSrvc == null)
        {
            mSrvc = new messagesService(FeedUrl);
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        XmlDocument xmlMessages = mSrvc.GetMessages();
     
        StringBuilder sBuilder = new StringBuilder();

        int MinValue = 0;
        int MaxValue = 0;
        decimal Scale = 1;

        foreach (XmlNode node in xmlMessages.DocumentElement.ChildNodes)
        {
            if (node.Name.Equals("statistics"))
            {
                int.TryParse(node.Attributes["MinValue"].Value, out MinValue);
                int.TryParse(node.Attributes["MaxValue"].Value, out MaxValue);
                Scale = MaxFont / MaxValue;
            }

            if (!node.Name.Equals("message"))
                continue;

            int FontSize = MinFont;

            int.TryParse(node.Attributes["Weight"].Value, out FontSize);

            FontSize = Convert.ToInt32(FontSize * Scale);

            if (FontSize < MinFont) FontSize = MinFont;
            if (FontSize > MaxFont) FontSize = MaxFont;

            sBuilder.Append(string.Format(
                messageFormat,
                FontSize,
                LinkUrl + node.Attributes["Text"].Value,
                //node.Attributes["Weight"].Value,
                node.Attributes["Text"].Value                
                ));
            
        }

        ph1.Controls.Add(new LiteralControl(sBuilder.ToString()));
    }

}
